package hydraperf.shared;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Christopher Bartling, Pintail Consulting LLC
 * @since Nov 27, 2008 3:02:26 PM
 */
public class Controller {

    private boolean started = false;
    private transient List<ChangeListener> changeListeners;

    public void fireChanged() {
        for (ChangeListener l : changeListeners) {
            l.stateChanged(new ChangeEvent(this));
        }
    }

    public void addChangeListener(ChangeListener l) {
        if (changeListeners == null) {
            changeListeners = new ArrayList<ChangeListener>();
        }
        changeListeners.add(l);
        fireChanged();
    }

    public void removeChangeListener(ChangeListener l) {
        changeListeners.remove(l);
    }

    public synchronized boolean isStarted() {
        return started;
    }

    public void start() {
        synchronized (this) {
            started = true;
        }
        fireChanged();
    }

    public void stop() {
        synchronized (this) {
            started = false;
        }
        fireChanged();
    }
}
